package com.softeem.score.manager.controller.admin;

import com.github.pagehelper.PageInfo;
import com.softeem.score.common.log.annotation.Log;
import com.softeem.score.common.log.enums.OperatorType;
import com.softeem.score.manager.service.MajorService;
import com.softeem.score.model.dto.MajorDto;
import com.softeem.score.model.entity.MajorEntity;
import com.softeem.score.model.entity.school.Major;
import com.softeem.score.model.vo.common.Result;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.school.CourseMajorVo;
import com.softeem.score.model.vo.school.MajorVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专业接口")
@RestController
@RequestMapping(value = "/admin/school/major")
public class MajorController {
    @Autowired
    private MajorService majorService;

    @Operation(summary = "分页查询专业列表")
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<Major>> findByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                              @PathVariable(value = "pageSize") Integer pageSize,
                                              MajorDto majorDto) {
        PageInfo<MajorVo> pageInfo = majorService.findByPage(majorDto, pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询所有专业信息")
    @GetMapping("/getMajorListByCollegeId")
    public Result<List<MajorEntity>> getMajorListByCollegeId(@RequestParam(value = "collegeId", required = false) Long collegeId) {
        List<MajorEntity> majors = majorService.getMajorListByCollegeId(collegeId);
        return Result.build(majors, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "保存专业信息", businessType = 1, operatorType = OperatorType.MANAGE)
    @Operation(summary = "保存专业信息")
    @PostMapping(value = "/saveMajor")
    public Result saveMajor(@RequestBody Major major) {
        majorService.saveMajor(major);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新专业信息", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "更新专业信息")
    @PutMapping(value = "/updateMajor")
    public Result updateMajor(@RequestBody Major major) {
        majorService.updateMajor(major);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "删除专业", businessType = 3, operatorType = OperatorType.MANAGE)
    @Operation(summary = "删除专业信息")
    @DeleteMapping(value = "/deleteMajorById/{majorId}")
    public Result deleteMajorById(@PathVariable(value = "majorId") Long majorId) {
        majorService.deleteMajorById(majorId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Log(title = "对专业分配课程", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "对专业分配课程")
    @PutMapping(value = "/doAssignCourse/{majorId}")
    public Result doAssignCourse(@PathVariable(value = "majorId") Long majorId,
                                 @RequestParam(name = "courseIds", required = false) List<Long> courseIds) {
        majorService.saveCourseToMajor(majorId, courseIds);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "查询当前专业所有分配课程id", businessType = 0, operatorType = OperatorType.MANAGE)
    @Operation(summary = "查询当前专业所有分配课程id")
    @GetMapping(value = "/getCheckCollege/{majorId}")
    public Result getCheckCollege(@PathVariable(value = "majorId") Long majorId) {
        List<Long> checkCollege = majorService.getCheckCollege(majorId);
        return Result.build(checkCollege, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "查询当前专业所有分配课程", businessType = 0, operatorType = OperatorType.MANAGE)
    @Operation(summary = "查询当前专业所有分配课程")
    @GetMapping(value = "/getCourseByMajoeId/{majorId}")
    public Result getCourseByMajoeId(@PathVariable(value = "majorId") Long majorId) {
        List<CourseMajorVo> checkCollege = majorService.getCourseByMajorId(majorId);
        return Result.build(checkCollege, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "更新专业课程", businessType = 2, operatorType = OperatorType.MANAGE)
    @Operation(summary = "更新专业课程")
    @PutMapping(value = "/updateCourseMajor")
    public Result updateCourseMajor(@RequestBody CourseMajorVo courseMajor) {
        majorService.updateCourseMajor(courseMajor);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
